﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace newTurtle8
{
    public partial class TahasukaSiurKavCard : UserControl
    {     
        Tahasuka tahasuka;
        DataTable dt = new DataTable();
        SqlDataAdapter da;
        
        public TahasukaSiurKavCard(Tahasuka tahasuka)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            this.tahasuka = tahasuka;

            da = SqlManager.SqlDataAdapterFactory(SqlManager.getSqlStringFromFile(@"\SELECT\TahasukaSiurKav.SELECT.sql"));
            da.UpdateCommand = SqlManager.SqlCommandFactory(SqlManager.getSqlStringFromFile(@"\UPDATE\TahasukaSiurKav.UPDATE.sql"));
           
            IEnumerable<string> sqlParameterCollection = SqlManager.parseSqlQueryParameters(da.UpdateCommand.CommandText).Distinct<string>();
            //adding parameters dynamically to the UPDATE SqlCommand
            foreach (string item in sqlParameterCollection)
                switch (item)
                {
                    case "startDate":                         
                        da.SelectCommand.Parameters.Add("@startDate", SqlDbType.Date).Value = tahasuka.StartDate;
                        da.UpdateCommand.Parameters.Add("@startDate", SqlDbType.Date).Value = tahasuka.StartDate;
                    break;

                    default: da.UpdateCommand.Parameters.Add(new SqlParameter("@" + item, SqlDbType.NVarChar));
                        break;
                }

            DataTable dt_temp = SqlManager.selectSqlQueryDataTable(da.SelectCommand);
            DataRow row = dt_temp.Rows[0];

            //sets each textBox data.
            textBoxGeneral.Text = row["general"].ToString();
            textBoxObjective.Text = row["objective"].ToString();
            textBoxForcesAndMissions.Text = row["forcesAndMissions"].ToString();
            textBoxRegionHistory.Text = row["regionalHistory"].ToString();
            textBoxEnemy.Text = row["enemy"].ToString();
            textBoxSafety.Text = row["safety"].ToString();
            textBoxLogistics.Text = row["logistics"].ToString();
            textBoxOther.Text = row["other"].ToString();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            //sets parameters values
            da.UpdateCommand.Parameters["@general"].Value = textBoxGeneral.Text;
            da.UpdateCommand.Parameters["@objective"].Value = textBoxObjective.Text;
            da.UpdateCommand.Parameters["@forcesAndMissions"].Value = textBoxForcesAndMissions.Text;
            da.UpdateCommand.Parameters["@regionalHistory"].Value = textBoxRegionHistory.Text;
            da.UpdateCommand.Parameters["@enemy"].Value = textBoxEnemy.Text;
            da.UpdateCommand.Parameters["@safety"].Value = textBoxSafety.Text;
            da.UpdateCommand.Parameters["@logistics"].Value = textBoxLogistics.Text;
            da.UpdateCommand.Parameters["@other"].Value = textBoxOther.Text;

            //execute Update command
            SqlManager.executeNonQuery(da.UpdateCommand);

            labelSuccessfulUpdate.Visible = true;
            labelSuccessfulUpdate.Location = new Point(splitContainer.Panel1.Bounds.Width / 2, 16);
            timer.Interval = 5 * 1000;
            timer.Start();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            labelSuccessfulUpdate.Visible = false;
        }
    }
}
